############################################
# Replication file for Entre Rios Province #
############################################


rm(list=ls(all=TRUE))
#setwd("")
library(eiPack)
library(coda)
library(foreign)

data=read.csv(file.path(getwd(),"data","ER_data.csv"))

# Define row and column marginals of the transition matrix. 
GFPV=data$gral_501
GUER=data$gral_545
GUCR=data$gral_3
GO=data$gral_68+data$gral_546
GN=data$votantes-GFPV-GUER-GUCR-GO


PFPVA=data$paso_3128
PFPVB=data$paso_3129
PUER=data$paso_545
PUCR=data$paso_3
PO=data$paso_3125+data$paso_3126+data$paso_3127+data$paso_68
PN=data$votantes-PFPVA-PFPVB-PUER-PUCR-PO

data1=data.frame(PFPVA,PFPVB,PUER,PUCR,PO,PN,GFPV,GUER,GUCR,GO,GN)



# Tunning the EI alrogirth
tune.nocov=tuneMD(cbind(GFPV,GUER,GUCR,GO,GN)~cbind(PFPVA,PFPVB,PUER,PUCR,PO,PN),
                  data=data1,ntunes=1,totaldraws=200000)

# MCMC
out.nocov=ei.MD.bayes(cbind(GFPV,GUER,GUCR,GO,GN)~cbind(PFPVA,PFPVB,PUER,PUCR,PO,PN),
                      covariate=NULL,data=data1,tune.list=tune.nocov,verbose=0,ret.beta="r",thin=80,burnin=100000,ret.mcmc=TRUE) 




# Obtain transition matrix for each mesa: 
source(file.path(getwd(),"Codes","getbetas.R"))
beta11=getbetas(out.nocov,"PFPVA","GFPV")
beta12=getbetas(out.nocov,"PFPVA","GUER")
beta13=getbetas(out.nocov,"PFPVA","GUCR")
beta14=getbetas(out.nocov,"PFPVA","GO")
beta15=getbetas(out.nocov,"PFPVA","GN")
beta21=getbetas(out.nocov,"PFPVB","GFPV")
beta22=getbetas(out.nocov,"PFPVB","GUER")
beta23=getbetas(out.nocov,"PFPVB","GUCR")
beta24=getbetas(out.nocov,"PFPVB","GO")
beta25=getbetas(out.nocov,"PFPVB","GN")
beta31=getbetas(out.nocov,"PUER","GFPV")
beta32=getbetas(out.nocov,"PUER","GUER")
beta33=getbetas(out.nocov,"PUER","GUCR")
beta34=getbetas(out.nocov,"PUER","GO")
beta35=getbetas(out.nocov,"PUER","GN")
beta41=getbetas(out.nocov,"PUCR","GFPV")
beta42=getbetas(out.nocov,"PUCR","GUER")
beta43=getbetas(out.nocov,"PUCR","GUCR")
beta44=getbetas(out.nocov,"PUCR","GO")
beta45=getbetas(out.nocov,"PUCR","GN")
beta51=getbetas(out.nocov,"PO","GFPV")
beta52=getbetas(out.nocov,"PO","GUER")
beta53=getbetas(out.nocov,"PO","GUCR")
beta54=getbetas(out.nocov,"PO","GO")
beta55=getbetas(out.nocov,"PO","GN")
beta61=getbetas(out.nocov,"PN","GFPV")
beta62=getbetas(out.nocov,"PN","GUER")
beta63=getbetas(out.nocov,"PN","GUCR")
beta64=getbetas(out.nocov,"PN","GO")
beta65=getbetas(out.nocov,"PN","GN")
betaout=data.frame(beta11,beta12,beta13,beta14,beta15,
                   beta21,beta22,beta23,beta24,beta25,
                   beta31,beta32,beta33,beta34,beta35,
                   beta41,beta42,beta43,beta44,beta45,
                   beta51,beta52,beta53,beta54,beta55,
                   beta61,beta62,beta63,beta64,beta65,
                   data$votantes,data$prov,data$comuna,data$circ,data$mesa)
write.table(betaout,file=file.path(getwd(),"Output","ER_Mesa.txt"),col.names=NA)



# Obtain aggregate transition matrix. 
source(file.path(getwd(),"Codes","getbetasagr.R"))
beta11=getbetasagr(out.nocov,"PFPVA","GFPV")
beta12=getbetasagr(out.nocov,"PFPVA","GUER")
beta13=getbetasagr(out.nocov,"PFPVA","GUCR")
beta14=getbetasagr(out.nocov,"PFPVA","GO")
beta15=getbetasagr(out.nocov,"PFPVA","GN")
beta21=getbetasagr(out.nocov,"PFPVB","GFPV")
beta22=getbetasagr(out.nocov,"PFPVB","GUER")
beta23=getbetasagr(out.nocov,"PFPVB","GUCR")
beta24=getbetasagr(out.nocov,"PFPVB","GO")
beta25=getbetasagr(out.nocov,"PFPVB","GN")
beta31=getbetasagr(out.nocov,"PUER","GFPV")
beta32=getbetasagr(out.nocov,"PUER","GUER")
beta33=getbetasagr(out.nocov,"PUER","GUCR")
beta34=getbetasagr(out.nocov,"PUER","GO")
beta35=getbetasagr(out.nocov,"PUER","GN")
beta41=getbetasagr(out.nocov,"PUCR","GFPV")
beta42=getbetasagr(out.nocov,"PUCR","GUER")
beta43=getbetasagr(out.nocov,"PUCR","GUCR")
beta44=getbetasagr(out.nocov,"PUCR","GO")
beta45=getbetasagr(out.nocov,"PUCR","GN")
beta51=getbetasagr(out.nocov,"PO","GFPV")
beta52=getbetasagr(out.nocov,"PO","GUER")
beta53=getbetasagr(out.nocov,"PO","GUCR")
beta54=getbetasagr(out.nocov,"PO","GO")
beta55=getbetasagr(out.nocov,"PO","GN")
beta61=getbetasagr(out.nocov,"PN","GFPV")
beta62=getbetasagr(out.nocov,"PN","GUER")
beta63=getbetasagr(out.nocov,"PN","GUCR")
beta64=getbetasagr(out.nocov,"PN","GO")
beta65=getbetasagr(out.nocov,"PN","GN")
betaoutagr=data.frame(beta11,beta12,beta13,beta14,beta15,
                   beta21,beta22,beta23,beta24,beta25,
                   beta31,beta32,beta33,beta34,beta35,
                   beta41,beta42,beta43,beta44,beta45,
                   beta51,beta52,beta53,beta54,beta55,
                   beta61,beta62,beta63,beta64,beta65,
                   data$votantes,data$prov,data$comuna,data$circ,data$mesa)

betaoutagr=as.vector(betaoutagr[1,1:120])
betaoutagr=matrix(betaoutagr,4)
betaoutagr_coefs=t(matrix(betaoutagr[1,],5))
betaoutagr_sd=t(matrix(betaoutagr[2,],5))
write.table(betaoutagr_coefs,file=file.path(getwd(),"Output","ERAggregate_Coefs.txt"),col.names=NA)
write.table(betaoutagr_sd,file=file.path(getwd(),"Output","ERAggregate_SD.txt"),col.names=NA)


# save(out.nocov, file = "ERMCMC.Rdata") # Saves the MCMC runs


# Geweke Convergence Statistics. 
geweke=geweke.diag(out.nocov$draws$Beta)
gewekestats=geweke[[1]]
gewekedf=data.frame(gewekestats)
#write.table(gewekedf,file="ERgewekelist.txt",col.names=NA)

jpeg(file.path(getwd(),"Output","ERHistogramGeweke.jpeg"))
hist(gewekestats)
dev.off()

print("Max Geweke")
print(max(gewekestats))
print("Min Geweke")
print(min(gewekestats))
print("Percentage Above 2.35")
print(length(gewekestats[abs(gewekestats)>2.35])/length(gewekestats))
print("Percentage Above 1.96")
print(length(gewekestats[abs(gewekestats)>1.96])/length(gewekestats))
print("Percentage Above 1.5")
print(length(gewekestats[abs(gewekestats)>1.5])/length(gewekestats))
print("Percentage Above 1.5")
print(length(gewekestats[abs(gewekestats)>1])/length(gewekestats))
print("Percentage Above 0.5")
print(length(gewekestats[abs(gewekestats)>0.5])/length(gewekestats))
